package com.cakin.newssystem.common;

import lombok.EqualsAndHashCode;
import lombok.Data;
import lombok.Builder;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @className: RequestException
 * @description: 定义一个运行时候异常
 * @date: 2020/5/6
 * @author: cakin
 */
@EqualsAndHashCode(callSuper = true)
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class RequestException extends RuntimeException implements Serializable {
    /**
     * 状态
     */
    private Integer status;
    /**
     * 消息
     */
    private String msg;
    /**
     * 异常
     */
    private Exception e;

    public RequestException( ResponseCode statusEnum, Exception e ) {
        this.status = statusEnum.getCode();
        this.msg = statusEnum.getMsg();
        this.e = e;
    }


    public RequestException( ResponseCode statusEnum ) {
        this.status = statusEnum.getCode();
        this.msg = statusEnum.getMsg();
    }

    public static synchronized RequestException fail( String msg ) {
        return RequestException.builder()
                .status(ResponseCode.FAIL.getCode())
                .msg(msg)
                .build();
    }

    public static synchronized RequestException fail( String msg, Exception e ) {
        return RequestException.builder()
                .status(ResponseCode.FAIL.getCode())
                .msg(msg)
                .e(e)
                .build();
    }

    public static synchronized RequestException fail( Integer code, String msg, Exception e ) {
        return RequestException.builder()
                .status(code)
                .msg(msg)
                .e(e)
                .build();
    }
}
